<?php

$extensions = array('.doc', '.docx', '.xls', '.xlsx', '.odf', '.ods', '.txt', '.log', '.jpg', '.png', '.pdf'); // specify the allowed extensions here

if(!empty($_FILES)) {
	if ($_POST["attach_story"] == null)
		$_POST["attach_story"] = $_COOKIE["username"];
		
	$upload_dir	= "./attachments/".$_POST["attach_group_id"]."/".$_POST["attach_story"]."/";
	
	$tmp_file	= $_FILES['userfile']['tmp_name'];
	$filename	= $_FILES['userfile']['name'];
	$newfile	= $upload_dir.$filename;
	
	if (file_exists($newfile))
		unlink($newfile);
	
	if (!is_dir($upload_dir)){
		mkdir($upload_dir, 0777, true);
	}
	if (move_uploaded_file($tmp_file, $newfile)){
		echo '<div id="status">success</div>';
		echo '<div id="message">'. $filename .' Successfully Uploaded</div>';
		
		$ext = preg_replace('/^.*\./', '', $filename);
		$json = array("type"=>$ext,"path"=>$newfile,"filename"=>$filename);
		
		echo '<div id="uploadedfile" filename="'.$filename.'">'. json_encode($json) .'</div>';
		
	} else {
		
		echo '<div id="status">failed</div>';
		echo '<div id="message">Failed to upload file.</div>';
		
	}
}
?>
